package yl.hs.bmipfull.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.ISysFileDao;
import yl.hs.bmipfull.pojo.system.SysFileEntity;
import yl.hs.bmipfull.service.system.IFileService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author 张凯
 * @apiNote 文件管理-ServiceImpl
 * @since 2022/1/21 8:58 上午 创建
 */
@Service
public class FileServiceImpl extends SystemBaseService implements IFileService {


    @Autowired
    ISysFileDao dao;

    @Override
    public List<SysFileEntity> queryList(PageQuery pageQuery)throws Exception{
        pageQuery.checkSortSides(SysFileEntity.class);
        return dao.queryPage(pageQuery.getQuery());
    }
    @Override
    public PageResult<SysFileEntity> queryPage(PageQuery pageQuery, int rowNum, int page)throws Exception{
        pageQuery.checkSortSides(SysFileEntity.class);
        pageQuery.getQuery().put("querySkip",(page-1)*rowNum);
        pageQuery.getQuery().put("pageSize",rowNum);
        PageResult<SysFileEntity> result=new PageResult(rowNum);
        result.setRecords(dao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(dao.queryPage(pageQuery.getQuery()));
        return result;
    }



    @Override
    public SysFileEntity findBySn(String sn){
        return dao.findBySN(sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer saveSysFile(Map<String, Object> model, String sn)throws Exception{
        Integer result=0;
        SysFileEntity entity=null;
        if(Tl.isEmpty(sn)){
            //执行保存
            entity=new SysFileEntity();
            Tl.getChangeLog(entity,model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(SysFileEntity.class));
            result=dao.insertNew(entity);
            if(result>0){
                writeLog(ActionType.CREATE,entity.getSn(),"插入系统文件信息",Tl.toJson(entity), SysFileEntity.class);
            }

        }else{
            //执行修改
            entity=findBySn(sn);
            Tl.getChangeLog(entity,model);
            entity.setPYFieldValue();
            result=dao.update(entity);
            if(result>0){
                writeLog(ActionType.UPDATE,sn,"更新系统文件信息",Tl.toJson(entity), SysFileEntity.class);
            }
        }

        return result;
    }



    @Override
    public String save(SysFileEntity sysFile) {
        try {
            sysFile.setSn(queryNewSN(SysFileEntity.class));
            dao.insertNew(sysFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysFile.getSn();
    }

    @Override
    public boolean delete(String sn) {
        SysFileEntity sysFile = dao.findBySN(sn);
        if (sysFile != null) {
            var file = new File(sysFile.getFilePath());
            if (file.exists()) {
                return file.delete();

            }
            dao.delete(sysFile.getSn());
        }

        return false;
    }
}
